<?
/*
    Classes to handle different Dataz widgets.
    Reads in dzwidget.ini.

    Features are grouped to separate modes (edit, browse and query). It
    seems that no more modes exist. Mode 'new' is a special case of the
    'edit' mode. That's why modes are hardcoded.

    This class is used from dzScriptSeppo and is strictly html specific.

    This collection of functions (libary) is licensed under GPL2.
    See LICENSE or www.gnu.org for more details.

    Author Henri Ranki 14.9.2000

    $Id: dzwidget.php 595 2006-04-03 16:00:47Z jyry $
*/

include_once("sodebug.php");
include_once("sostring.php");
include_once("soset.php");
include_once("sogeneric.php");
include_once("soinifiles.php");
include_once("dzconst.php");


/** Stores information for one widget type.
 *  Includes the list of JS features and properties needed to
 *  implement the widget
 */
class dzWidget {
    var $Type;

    var $EditFeatures;
    var $EditPropeties;

    var $BrowseFeatures;
    var $BrowsePropeties;

    var $QueryFeatures;
    var $QueryPropeties;

    var $DefaultFeatures;
    var $DefaultPropeties;


    /** Dumps the features of this widget
     *  @param string $Delimiter String that delimits different features
     *      from each other
     *  @return string Class data dumped into a string
     */
    function dump($Delimiter = "<br>") {
        $result = "EditFeatures=".$this->EditFeatures->dump(",").$Delimiter.
               "BrowseFeatures=".$this->BrowseFeatures->dump(",").$Delimiter.
               "QueryFeatures=".$this->QueryFeatures->dump(",").$Delimiter.
               "DefaultFeatures=".$this->DefaultFeatures->dump(",");

        return $result;
    } // dump

} // dzWidget


/** Set of possible widgets
 *  Used by dzScriptSeppo to store the possible widget and their
 *  JS features and properties
 *
 *  @var object soSet $Widgets Widget base.
 */
class dzWidgetSet extends soBaseClass {

    var $Widgets;

    /** Constructor
     *  Initialises class. Reads in the dzwidget.ini
     *  @param string $IniStream Input stream to read ini from.
     */
    function dzWidgetSet($IniStream = "") {
        global $_DZ_GLOBALS;

        /* no specific ini stream specified? use default then */
        if (!$IniStream) {
            $IniStream = "file://".dzGetDataxiPath()."/".
                          soExtractVal(basename(__FILE__), ".", 1).".ini";
        }; // if

        $ini_file = $_DZ_GLOBALS[DZGV_OBJECTS][$IniStream];
        if ($ini_file == NULL) {
            $ini_file = new soIniData($IniStream);
            $_DZ_GLOBALS[DZGV_OBJECTS][$IniStream] = $ini_file;
        };
        $this->Widgets = new soSet();

        /* Check that the ini file was found */
        if ($ini_file->Error) {
            dzSysError("Error: Could not find dzwidget.ini: $IniStream");
            $this->Error = true;
        } else {
            $this->Error = false;
        } // if

        $SectionCount = $ini_file->getSectionCount();
        soDebug("dzWidgetSet/constructor: SectionCount=$SectionCount", SOD_DET);

        $widgets = &$_DZ_GLOBALS[DZGV_OBJECTS][DZGV_WIDGETS];
        for ($c = 0; $c < $SectionCount; $c++) {
            $name = $ini_file->getSectionName($c);
            $NewComp = $widgets[$name];
            if (!$NewComp) {
                $NewComp = new dzWidget();

                $NewComp->Type = $ini_file->getValue($c, "type");

                $NewComp->BrowseFeatures =
                    $this->getFeatureSet($c, "browsefeatures", $ini_file);
                $NewComp->BrowseProperties =
                    $ini_file->getValueSet($c, "browseproperties");
                $NewComp->EditFeatures =
                    $this->getFeatureSet($c, "editfeatures", $ini_file);
                $NewComp->EditProperties =
                    $ini_file->getValueSet($c, "editproperties");
                $NewComp->QueryFeatures =
                    $this->getFeatureSet($c, "queryfeatures", $ini_file);
                $NewComp->QueryProperties =
                    $ini_file->getValueSet($c, "queryproperties");
                $NewComp->DefaultFeatures =
                    $this->getFeatureSet($c, "defaultfeatures", $ini_file);
                $NewComp->DefaultProperties =
                    $ini_file->getValueSet($c, "defaultproperties");
                $widgets[$name] = $NewComp;
            }; // if
            $this->Widgets->setItem($name, $NewComp);
        }; // for

        $_DZ_GLOBALS[DZGV_OBJECTS][DZGV_WIDGETS] = $widgets;
    } // constructor


    /** Read set of features from an ini file
     *  This is used only by constructor
     *  @param string $Name Widged name to read (section)
     *  @param string $KeyName Key name for the data
     *  @param object dzIniData $IniStr Ini file to read from
     *  @return object soSet Widget features
     */
    function getFeatureSet($Name, $KeyName, &$IniFile) {
        $ItemSet = new soSet();
        $IniStr = $IniFile->getValue($Name, $KeyName);
        $IniStr = ltrim(trim($IniStr));
        $ItemSet->joinArrayAsKeys(soExplode($IniStr, " "));
        return $ItemSet;
    } // getFeatureSet


    /** Return the required features for a widget in the defined mode.
     *  If widget was not given return only default features
     *  @param string >$WidgetName Widget which features to get
     *  @param string $Mode The mode
     *  @return soSet Required features
     */
    function getRequiredFeats($WidgetName, $Mode) {
        $Widget = $this->Widgets->getItem($WidgetName);

        /* If widget was given, check that the widget exist.*/
        if (($WidgetName != "") && (!is_object($Widget))) return 0;

        /* Get features from default section as well */
        $DefWidget = $this->Widgets->getItem("default");

        $result = new soSet();
        /* General default features, that apply to all widgets */
        $result->joinSet($DefWidget->DefaultFeatures);
        $result->joinSet($Widget->DefaultFeatures, 1);

        switch ($Mode) {
            case DZM_NEW:
            case DZM_EDIT:
                $result->joinSet($Widget->EditFeatures, 1);
                $result->joinSet($DefWidget->EditFeatures, 1);
                break;
            case DZM_LOOKUP:
            case DZM_BROWSE:
                $result->joinSet($Widget->BrowseFeatures, 1);
                $result->joinSet($DefWidget->BrowseFeatures, 1);
                break;
            case DZM_QUERY:
                $result->joinSet($Widget->QueryFeatures, 1);
                $result->joinSet($DefWidget->QueryFeatures, 1);
                break;
            case DZM_NONE:
                break;
        };

        return $result;
    } // getRequiredFeats

    
    /**
     * Return the required properties for a widget in the defined mode.
     * >$WidgetName:str Widget which features to get
     * >$Mode:str   The mode
     * <:soSet  needed properties
     */
    function getRequiredProps($WidgetName, $Mode) {
        $Widget = $this->Widgets->getItem($WidgetName);
        /* Check that the given widget exist */
        if (!is_object($Widget)) return 0;

        /* Let's take default properties as well */
        $DefWidget = $this->Widgets->getItem("default");

        $result = new soSet();
        /* General default properties, that apply to all widgets */
        $result->joinSet($DefWidget->DefaultProperties);

        switch ($Mode) {
            case DZM_NEW:
            case DZM_EDIT:
                $result->joinSet($DefWidget->EditProperties, 1);
                $result->joinSet($Widget->DefaultProperties, 1);
                $result->joinSet($Widget->EditProperties, 1);
                break;
            case DZM_LOOKUP:
            case DZM_BROWSE:
                $result->joinSet($DefWidget->BrowseProperties, 1);
                $result->joinSet($Widget->DefaultProperties, 1);
                $result->joinSet($Widget->BrowseProperties, 1);
                break;
            case DZM_QUERY:
                $result->joinSet($DefWidget->QueryProperties, 1);
                $result->joinSet($Widget->DefaultProperties, 1);
                $result->joinSet($Widget->QueryProperties, 1);
                break;
        }
        return $result;
    } // getRequiredProps


    /** Check widgets existence
     *  >$Name:str  Widget to check
     *  <:bool true if widget is defined otherwice false
     */
    function widgetExist($Name) {
        return $this->Widgets->itemExists($Name);
    }

} // class dzWidgetSet
?>
